<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :todoObjAdd="addTodo"></Header>
      <Main :todoList="list" :checkTodo="checkTodo" :deleteTodo="deleteTodo"></Main>
      <Footer :todoList="list" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"></Footer>
    </div>
  </div>
</template>

<script>
import Header from "@/components/TodoList/Header";
import Main from "@/components/TodoList/Main";
import Footer from "@/components/TodoList/Footer";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { Header, Main, Footer },
  data() {
    //这里存放数据
    return {
      list: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    addTodo(todoObj) {
      this.list.unshift(todoObj);
    },
    //勾选or取消勾选一个todo
    checkTodo(id) {
      this.list.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    //删除一个todo
    deleteTodo(id) {
      this.list = this.list.filter((todo) => todo.id !== id);
    },
    //全选or取消全选
    checkAllTodo(done) {
      this.list.forEach((todo) => {
        todo.done = done;
      });
    },
    //清除所有已经完成的todo
    clearAllTodo() {
      this.list = this.list.filter((todo) => {
        return !todo.done;
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>


<style scoped>
/* @import url(); 引入公共css类 */
body {
  background: #fff;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>